/**
 * 推广员系统
 */
import Vue from 'vue'

const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage/promoter'

const promoterListResource = Vue.resource(API_ROOT + '/list')
export const getPromoterList = ({dispatch}, params) => {
  return promoterListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PROMOTER_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const auditListResource = Vue.resource(API_ROOT + '/audit/list')
export const getPromoterAuditList = ({dispatch}, params) => {
  return auditListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PROMOTER_AUDIT_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const auditDetailResource = Vue.resource(API_ROOT + '/audit/detail')
export const getPromoterAuditDetail = ({dispatch}, params) => {
  return auditDetailResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const auditResource = Vue.resource(API_ROOT + '/audit')
export const auditPromoter = ({dispatch}, params) => {
  return auditResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PROMOTER_AUDIT_LIST_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const promoterRuleAddResource = Vue.resource(API_ROOT + '/rule/add')
export const addPromoterRule = ({dispatch}, params) => {
  return promoterRuleAddResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PROMOTER_RULE_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const promoterRuleListResource = Vue.resource(API_ROOT + '/rule/list')
export const getPromoterRuleList = ({dispatch}, params) => {
  return promoterRuleListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PROMOTER_RULE_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const promoterRuleDeleteResource = Vue.resource(API_ROOT + '/rule/del')
export const deletePromoterRule = ({dispatch}, params) => {
  return promoterRuleDeleteResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PROMOTER_RULE_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const promoterSettleImportResource = Vue.resource(API_ROOT + '/settle/import')
export const importPromoterSettle = ({dispatch}, params) => {
  return promoterSettleImportResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('PROMOTER_RULE_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const promoterSettleDetailListResource = Vue.resource(API_ROOT + '/pay/detail')
export const getSettleDetailList = ({dispatch}, params) => {
  return promoterSettleDetailListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const promoterVerifyResource = Vue.resource(API_ROOT + '/verify')
export const promoterVerify = ({dispatch}, params) => {
  return promoterVerifyResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const promoterShareListResource = Vue.resource(API_ROOT + '/share/list')
export const getShareList = ({dispatch}, params) => {
  return promoterShareListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const exportSettlePromotertResource = API_ROOT + '/settle/export'

const exportPayPromotertResource = Vue.resource(API_ROOT + '/update/status')
export const exportPayPromotert = ({dispatch}, params) => {
  return exportPayPromotertResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('PROMOTER_LIST_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const updateBannerResource = Vue.resource(API_ROOT + '/new/msg')
export const updateBanner = ({dispatch}, params) => {
  return updateBannerResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
const getBannerResource = Vue.resource(API_ROOT + '/get/msg')
export const getBanner = ({dispatch}, params) => {
  return getBannerResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
